﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

/*
 * https://social.msdn.microsoft.com/Forums/vstudio/en-US/d6a2836a-d587-4068-8630-94f4fb2a2aeb/encrypt-and-decrypt-a-string-in-c?forum=csharpgeneral
 * http://www.newtonsoft.com/json/help/html/serializedictionary.htm
*/

namespace MostRecentlyUsed.Models.EventArguments
{
    /// <summary>
    /// Connection String Menu Item
    /// </summary>
    [Serializable]
    public class SelectedConnectionString : EventArgs
    {
        /// <summary>
        /// Inializes a new instance of the <see cref="SelectedConnectionString"/> class
        /// </summary>
        /// <param name="connectionString">Connection string</param>
        /// <param name="provider">Provider used to make a database connection</param>
        /// <param name="itemsToReplace">String array of RexEx expressions that will be replaced when creating the <see cref="DisplayName"/> from the <see cref="ConnectionString"/></param>
        /// <param name="displayName">Value to display; if not supplied a display name will be constructed from the connectionString property removing passwords</param>
        public SelectedConnectionString(string connectionString, string provider, string[] itemsToReplace, string displayName = null)
        {
            ConnectionString = connectionString;
            string displayNameInner = connectionString;

            if (displayName == null)
            {
                foreach (var itemToReplace in itemsToReplace)
                {
                    var regEx = new Regex(itemToReplace);
                    displayNameInner = regEx.Replace(displayNameInner, string.Empty);
                }
            }
            else
            {
                displayNameInner = displayName;
            }

            DisplayName = displayNameInner;
            Provider = provider;
        }

        /// <summary>
        /// Value seen in the most recent list
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// Actual value to be used to connect to the database
        /// </summary>
        public string ConnectionString { get; set; }

        /// <summary>
        /// Database provider used for the connection
        /// </summary>
        public string Provider { get; set; }

        /// <summary>
        /// Ordinal value of the item
        /// </summary>
        public int Order { get; set; }
    }
}
